package com.dianping.cat.home.group.transform;

import com.dianping.cat.home.group.IVisitor;
import com.dianping.cat.home.group.entity.Domain;
import com.dianping.cat.home.group.entity.DomainGroup;
import com.dianping.cat.home.group.entity.Group;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;



public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(DomainGroup domainGroup) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(domainGroup, out);
    return out.toByteArray();
  }


  public static void build(DomainGroup domainGroup, OutputStream out) { domainGroup.accept(new DefaultNativeBuilder(out)); }



  public void visitDomain(Domain domain) {
/*  41 */     if (domain.getId() != null) {
/*  42 */       writeTag(1, 1);
/*  43 */       writeString(domain.getId());
    }

/*  46 */     if (!domain.getGroups().isEmpty()) {
/*  47 */       writeTag(33, 2);
/*  48 */       writeInt(domain.getGroups().size());

/*  50 */       for (Group group : domain.getGroups().values()) {
/*  51 */         group.accept(this.m_visitor);
      }
    }

/*  55 */     writeTag(63, 3);
  }


  public void visitDomainGroup(DomainGroup domainGroup) {
/*  60 */     writeTag(63, 0);

/*  62 */     if (!domainGroup.getDomains().isEmpty()) {
/*  63 */       writeTag(33, 2);
/*  64 */       writeInt(domainGroup.getDomains().size());

/*  66 */       for (Domain domain : domainGroup.getDomains().values()) {
/*  67 */         domain.accept(this.m_visitor);
      }
    }

/*  71 */     writeTag(63, 3);
  }


  public void visitGroup(Group group) {
/*  76 */     if (group.getId() != null) {
/*  77 */       writeTag(1, 1);
      writeString(group.getId());
    }

/*  81 */     if (group.getIps() != null) {
/*  82 */       writeTag(2, 2);
/*  83 */       writeInt(group.getIps().size());

/*  85 */       for (String ip : group.getIps()) {
/*  86 */         writeString(ip);
      }
    }

/*  90 */     writeTag(63, 3);
  }

  private void writeInt(int value) {
    try {
      writeVarint(value & 0xFFFFFFFFL);
/*  96 */     } catch (IOException e) {
/*  97 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 103 */       this.m_out.writeUTF(value);
/* 104 */     } catch (IOException e) {
/* 105 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 111 */       this.m_out.writeByte((field << 2) + type);
/* 112 */     } catch (IOException e) {
/* 113 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 119 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 120 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 123 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 124 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/group/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */